NAMESPACE="namespace"
APP="app"
VALUESFILE="values_k8s.yaml"
STANDFILE="stand_k8s.yaml"
TARGETFILE="values_final.yaml"
APPENDTIMES=0

k8snotation=$(cat <<EOF
metadata:
  name: values
kind: values
EOF
)
line_count=$(echo -e "$k8snotation" | wc -l)

cd $NAMESPACE
cp $APP.yaml $VALUESFILE
cp ../$APP.yaml $STANDFILE


cat <<EOF > "kustomization.yaml"
resources:
  - stand_k8s.yaml
patches:
  - path: values_k8s.yaml
EOF



if grep -Fxq "$k8snotation" $VALUESFILE; then
    echo "Content of k8snotation exists in $VALUESFILE."
else
    echo "Content of k8snotation does not exist in $VALUESFILE."
    echo -e "\n$k8snotation" >> "$VALUESFILE"
    echo "Text appended to the file."
    APPENDTIMES=$((APPENDTIMES + 1))
fi

if grep -Fxq "$k8snotation" $STANDFILE; then
    echo "Content of $k8snotation exists in $STANDFILE."
else
    echo "Content of $k8snotation does not exist in $STANDFILE."
    echo -e "\n$k8snotation" >> "$STANDFILE"
    echo "Text appended to the file."
    APPENDTIMES=$((APPENDTIMES + 1))
fi

if [ "$APPENDTIMES" -gt 1 ]; then
    kubectl kustomize | head -n -$line_count | tee $APP.yaml
else
    kubectl kustomize | tee $APP.yaml
fi


